<?php
 /**
 * Date
 * Samble Date class file.
 *
 * @author Lim Afriyadi
 * @version pre-alpha
 * @package Samble
 * @subpackage xsl
 * @license GNU/GPL.
 */
class SAMBLE_XSL
{
    private static $oInstance;
    private $sXML;
    private $fXML;
    private $sXSL;
    private $fXSL;
    private $oXslt_Processor;

    private function __CONSTRUCT()
    {
        $this->fXML = new DOMDocument();
        $this->fXSL = new DOMDocument();
        $this->oXslt_Processor = new XSLTProcessor();
        $this->loadPHP();
    }

    public static function getInstance()
    {
        if(!self::$oInstance)
        {
            self::$oInstance = new SAMBLE_XSL();
        }
        return self::$oInstance;
    }

    public function setXML($sXML)
    {
        if($this->fXML->load($sXML))
        {
            $this->sXML = $sXML;
            return true;
        }
        else if ($this->fXML->loadXML($sXML))
        {
            $this->sXML = $sXML;
            return true;
        }
        return false;
    }

    public function getXML()
    {
        return $this->sXML;
    }

    public function setXSL($sXSL)
    {
        if(!$this->fXSL->load($sXSL)) return false;
        $this->sXSL = $sXSL;
        return true;
    }

    public function getXSL()
    {
        return $this->sXSL;
    }

    public function xslt($sXML='',$sXSL='')
    {
        /* True :
         * IF (EITHER $sXML or $this->sXML NOT empty) AND (EITHER $sXSL or $this->sXSL NOT empty).
         */
        if(!empty($sXML))
        {
            $this->setXML($sXML);
        }
        if(!empty ($sXSL))
        {
            $this->setXSL($sXSL);
        }
        if(empty($this->sXSL) && empty($sXSL)) return false;
        if(!$this->oXslt_Processor->importStylesheet($this->fXSL))
        {
            ErrorHandler::error("Cannot load XML stylesheet <xsl> file.");
            return false;
        }
        $oXML = $this->oXslt_Processor->transformToXml($this->fXML);
        if(!$oXML)
        {
            ErrorHandler::error("Cannot process XML file.");
        }
        return $oXML;
        return true;
    }

    public function loadPHP()
    {
        if($this->oXslt_Processor instanceof XSLTProcessor)
        {
            $this->oXslt_Processor->registerPHPFunctions();
        }
    }
}
?>
